﻿@page "/Bas/Goods/Index"
@using nERP.ViewModel.Bas.GoodsVMs
@using System.ComponentModel.DataAnnotations
@inherits BasePage
@attribute [ActionDescription("_Page.Bas.Goods.Index", "nERP.Bas.Controllers,Goods")]

<Table @ref="GoodsListVMdataTable" TItem="nERP.ViewModel.Bas.GoodsVMs.Goods_View" OnQueryAsync="GoodsListVMOnSearchGoods"
       IsStriped="true" IsBordered="true" ShowRefresh="false" ShowExtendEditButton="false" ShowExtendDeleteButton="false"
       ShowToolbar="true" IsMultipleSelect="true" ShowExtendButtons="true"
       AllowResizing="true" ShowDefaultButtons="false" IsPagination="true" AllowDragColumn="true" ShowColumnList="true" ShowSearch="true" SearchTooltip=""
       OnResetSearchAsync="OnResetSearchAsync" SearchMode="SearchMode.Top" CollapsedTopSearch="true" ShowSearchText="false">
    <SearchTemplate>
        <ValidateForm Model="@Model">
            <Row ItemsPerRow="ItemsPerRow.Three" RowType="RowType.Inline">
                <BootstrapInput @bind-Value="@Model.Searcher.GoodsCode" />
                <BootstrapInput @bind-Value="@Model.Searcher.GoodsName" />
                <Select @bind-Value="@Model.Searcher.GoodsTypeId" Items="AllGoodsTypes" PlaceHolder="@WtmBlazor.Localizer["Sys.All"]" />
            </Row>
        </ValidateForm>
    </SearchTemplate>
    <TableColumns>
        <TableColumn @bind-Field="@context.Goods_GoodsCode" Text="@WtmBlazor.Localizer["Page.商品编号"]" />
        <TableColumn @bind-Field="@context.Goods_GoodsName" Text="@WtmBlazor.Localizer["Page.商品名称"]" />
        <TableColumn @bind-Field="@context.Goods_GoodsSpec" Text="@WtmBlazor.Localizer["Page.规格型号"]" />
        <TableColumn @bind-Field="@context.Goods_GoodsType" Text="@WtmBlazor.Localizer["Page.商品类别"]" />
        <TableColumn @bind-Field="@context.Goods_GoodsUnit" Text="@WtmBlazor.Localizer["Page.计量单位"]" />
        <TableColumn @bind-Field="@context.Goods_GoodsImages" ShownWithBreakPoint="BreakPoint.Medium" Text="@WtmBlazor.Localizer["_Model._Goods._GoodsImages"]">
            <Template Context="data">
                @if (data.Value != null && data.Value?.Count() > 0)
                {
                    <ImageViewer Url="@picUrl(data.Value)" PreviewList="@rvPic(data.Value)" class="item-image" />
                }
            </Template>
        </TableColumn>
        <TableColumn @bind-Field="@context.Goods_RestockingPrice" Text="@WtmBlazor.Localizer["Page.进货价"]" FormatString="0.##" />
        <TableColumn @bind-Field="@context.Goods_RetailPrice" Text="@WtmBlazor.Localizer["Page.零售价"]" FormatString="0.##" />
        <TableColumn @bind-Field="@context.Goods_WholesalePrice" Text="@WtmBlazor.Localizer["Page.批发价"]" FormatString="0.##" />
        <TableColumn @bind-Field="@context.Goods_SafetyStock" Text="@WtmBlazor.Localizer["Page.安全库存"]" FormatString="0.##" />
        <TableColumn @bind-Field="@context.Goods_Remark" Text="@WtmBlazor.Localizer["_Admin.Remark"]" />
        <TableColumn @bind-Field="@context.Goods_Stock" Text="@WtmBlazor.Localizer["Page.库存"]" FormatString="0.##" />
        <TableColumn @bind-Field="@context.Goods_CreateTime" Text="@WtmBlazor.Localizer["_Admin.CreateTime"]" FormatString="yyyy-MM-dd HH:mm:ss" />
        <TableColumn @bind-Field="@context.Goods_UpdateTime" Text="@WtmBlazor.Localizer["_Admin.UpdateTime"]" FormatString="yyyy-MM-dd HH:mm:ss" />
        <TableColumn @bind-Field="@context.Goods_CreateBy" Text="@WtmBlazor.Localizer["_Admin.CreateBy"]" />
        <TableColumn @bind-Field="@context.Goods_UpdateBy" Text="@WtmBlazor.Localizer["_Admin.UpdateBy"]" />
        <TableColumn @bind-Field="@context.Goods_IsValid" Text="@WtmBlazor.Localizer["_Admin.IsValid"]" />
    </TableColumns>

    <RowButtonTemplate>
        @if (IsAccessable("/api/Bas/Goods/Edit"))
        {
            <TableCellButton Size="Size.ExtraSmall" Icon="fa fa-pencil-square" Color="Color.Warning" Text="@WtmBlazor.Localizer["Sys.Edit"]" OnClick="()=>OnEditClick(context)" />
        }
        @if (IsAccessable("/api/Bas/Goods/{id}"))
        {
            <TableCellButton Size="Size.ExtraSmall" Icon="fa fa-info-circle" Color="Color.Info" Text="@WtmBlazor.Localizer["Page.详情"]" OnClick="()=>OnDetailsClick(context)" />
        }
        @if (IsAccessable("/api/Bas/Goods/BatchDelete"))
        {
            <PopConfirmButton OnConfirm="() => OnDeleteClick(context)" Size="Size.ExtraSmall" Icon="fa fa-trash" Color="Color.Danger" Text="@WtmBlazor.Localizer["Sys.Delete"]" Content="@WtmBlazor.Localizer["Sys.DeleteConfirm"]" CloseButtonText="@WtmBlazor.Localizer["Sys.Close"]" ConfirmButtonText="@WtmBlazor.Localizer["Sys.Delete"]"
                              ConfirmButtonColor="Color.Danger" />
        }
    </RowButtonTemplate>
    <TableToolbarTemplate>
        @if (IsAccessable("/api/Bas/Goods/Create"))
        {
            <TableToolbarButton TItem="nERP.ViewModel.Bas.GoodsVMs.Goods_View" Icon="fa fa-plus" Color="Color.Primary" Text="@WtmBlazor.Localizer["Sys.Create"]" OnClickCallback="(x)=>OnCreateClick()" />
        }
        @if (IsAccessable("/api/Bas/Goods/BatchDelete"))
        {
            <TableToolbarPopConfirmButton TItem="nERP.ViewModel.Bas.GoodsVMs.Goods_View"
                                          Icon="fa fa-trash" Color="Color.Danger" Text="@WtmBlazor.Localizer["Sys.Delete"]" Content="@WtmBlazor.Localizer["Sys.BatchDeleteConfirm"]" CloseButtonText="@WtmBlazor.Localizer["Sys.Close"]" ConfirmButtonText="@WtmBlazor.Localizer["Sys.BatchDelete"]"
                                          OnConfirm="@OnBatchDeleteClick" ConfirmButtonColor="Color.Danger" />
        }
        @if (IsAccessable("/api/Bas/Goods/BatchEdit"))
        {
            <TableToolbarButton TItem="nERP.ViewModel.Bas.GoodsVMs.Goods_View" Icon="fa fa-pencil-square" Color="Color.Primary" Text="@WtmBlazor.Localizer["Sys.BatchEdit"]" OnClickCallback="(x)=>OnBatchEditClick()" />
        }
        @if (IsAccessable("/api/Bas/Goods/Import"))
        {
            <TableToolbarButton TItem="nERP.ViewModel.Bas.GoodsVMs.Goods_View" Icon="fa fa-tasks" Color="Color.Primary" Text="@WtmBlazor.Localizer["Sys.Import"]" OnClickCallback="(x)=>OnImportClick()" />
        }
        @if (IsAccessable("/api/Bas/Goods/GoodsExportExcel"))
        {
            <TableToolbarButton TItem="nERP.ViewModel.Bas.GoodsVMs.Goods_View" Icon="fa fa-arrow-circle-down" Color="Color.Primary" Text="@WtmBlazor.Localizer["Sys.Export"]" OnClickCallback="(x)=>OnExportClick()" IsAsync="true" />
        }
    </TableToolbarTemplate>

</Table>



@code {
    [Parameter]
    public string id { get; set; }
    [Parameter]
    public string[] ids { get; set; }
    private GoodsListVM Model = new GoodsListVM();
    private Table<nERP.ViewModel.Bas.GoodsVMs.Goods_View> GoodsListVMdataTable;
    private Task OnResetSearchAsync(Goods_View item)
    {
        Model.Searcher = new GoodsSearcher();
        item = new Goods_View();
        return Task.CompletedTask;
    }
    private async Task GoodsListVMDoSearch()
    {
        await GoodsListVMdataTable.QueryAsync();
    }
    private async Task<QueryData<nERP.ViewModel.Bas.GoodsVMs.Goods_View>> GoodsListVMOnSearchGoods(QueryPageOptions opts)
    {

        return await StartSearch<nERP.ViewModel.Bas.GoodsVMs.Goods_View>("/api/Bas/Goods/SearchGoods", Model.Searcher, opts);
    }
    private async Task OnCreateClick()
    {
        var id = GoodsListVMdataTable.SelectedRows?.Select(x => x.ID.ToString())?.FirstOrDefault();
        if (await OpenDialog<Create>(@WtmBlazor.Localizer["Sys.Create"], x => x.id == (id ?? ""), isMax: false) == DialogResult.Yes)
        {
            await GoodsListVMdataTable.QueryAsync();
        }
    }

    private async Task OnEditClick(nERP.ViewModel.Bas.GoodsVMs.Goods_View item)
    {
        if (await OpenDialog<Edit>(@WtmBlazor.Localizer["Sys.Edit"], x => x.id == item.ID.ToString(), isMax: false) == DialogResult.Yes)
        {
            await GoodsListVMdataTable.QueryAsync();
        }
    }

    private async Task OnDetailsClick(nERP.ViewModel.Bas.GoodsVMs.Goods_View item)
    {
        if (await OpenDialog<Details>(@WtmBlazor.Localizer["Page.详情"], x => x.id == item.ID.ToString(), isMax: false) == DialogResult.Yes)
        {
            await GoodsListVMdataTable.QueryAsync();
        }
    }

    private async Task OnDeleteClick(nERP.ViewModel.Bas.GoodsVMs.Goods_View item)
    {
        await PostsData(new List<string> { item.ID.ToString() }, $"/api/Bas/Goods/batchdelete", (s) => "Sys.OprationSuccess");
        await GoodsListVMdataTable.QueryAsync();
    }

    private async Task OnBatchDeleteClick()
    {
        if (GoodsListVMdataTable.SelectedRows?.Any() == true)
        {
            await PostsData(GoodsListVMdataTable.SelectedRows.Select(x => x.ID.ToString()).ToList(), $"/api/Bas/Goods/batchdelete", (s) => WtmBlazor.Localizer["Sys.BatchDeleteSuccess", s]);
            await GoodsListVMdataTable.QueryAsync();
        }
        else
        {
            await WtmBlazor.Toast.Information(WtmBlazor.Localizer["Sys.Info"], WtmBlazor.Localizer["Sys.SelectOneRowMin"]);
        }
    }

    private async Task OnBatchEditClick()
    {
        if (GoodsListVMdataTable.SelectedRows?.Any() == true)
        {
            if (await OpenDialog<BatchEdit>(WtmBlazor.Localizer["Sys.BatchEdit"], x => x.ids == GoodsListVMdataTable.SelectedRows.Select(x => x.ID.ToString()).ToArray(), isMax: false) == DialogResult.Yes)
            {
                await GoodsListVMdataTable.QueryAsync();
            }
        }
        else
        {
            await WtmBlazor.Toast.Information(WtmBlazor.Localizer["Sys.Info"], WtmBlazor.Localizer["Sys.SelectOneRowMin"]);
        }
    }

    private async Task OnImportClick()
    {
        var id = GoodsListVMdataTable.SelectedRows?.Select(x => x.ID.ToString())?.FirstOrDefault();
        if (await OpenDialog<Import>(@WtmBlazor.Localizer["Sys.Import"], x => x.id == (id ?? ""), isMax: false) == DialogResult.Yes)
        {
            await GoodsListVMdataTable.QueryAsync();
        }
    }

    private async Task OnExportClick()
    {
        if (GoodsListVMdataTable.SelectedRows?.Any() == true)
        {
            await Download("/api/Bas/Goods/GoodsExportExcelByIds", GoodsListVMdataTable.SelectedRows.Select(x => x.ID.ToString()).ToList());
        }
        else
        {
            await Download("/api/Bas/Goods/GoodsExportExcel", Model.Searcher);
        }
    }


    private List<SelectedItem> AllGoodsTypes = new List<SelectedItem>();
    private List<SelectedItem> AllGoodsUnits = new List<SelectedItem>();
    protected override async Task OnInitializedAsync()
    {


        AllGoodsTypes = await WtmBlazor.Api.CallItemsApi("/api/Bas/Goods/GetGoodsClasss");
        AllGoodsTypes.Insert(0, new SelectedItem { Text = WtmBlazor.Localizer["Sys.PleaseSelect"], Value = "" });
        AllGoodsUnits = await WtmBlazor.Api.CallItemsApi("/api/Bas/Goods/GetComputationUnits");
        AllGoodsUnits.Insert(0, new SelectedItem { Text = WtmBlazor.Localizer["Sys.PleaseSelect"], Value = "" });

        await base.OnInitializedAsync();
    }

    private string picUrl(List<Guid> pic)
    {
        return "/api/_file/DownloadFile/" + pic.ElementAt(0);
    }
    private List<string> rvPic(List<Guid> pic)
    {
        var picList = new List<string>();
        foreach (Guid s in pic)
        {
            picList.Add("/api/_file/DownloadFile/" + s.ToString());
        }
        return picList;// new List<string>() { "/api/_file/DownloadFile/" + pic };
    }
}
